package com.euro.video.phone.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.euro.video.phone.dao.DevisDao;
import com.euro.video.phone.entities.Devis;

@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class DevisDaoImpl implements DevisDao {
	@PersistenceContext
	private EntityManager manager;

	public Devis creerDevis(Devis devis) throws Exception {
		manager.persist(devis);
		return devis;
	}

	public Devis lireDevis(long id) throws Exception {
		return manager.find(Devis.class, id);
	}

	public Devis modifierDevis(Devis devis) throws Exception {
		return manager.merge(devis);
	}

	public boolean supprimerDevis(Devis devis) throws Exception {
		try {
			manager.remove(manager.contains(devis) ? devis : manager.merge(devis));
			return true;
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Devis> tousLesDevisNonTraites() throws Exception {
		Query query = manager.createNamedQuery("devisNonTraites");
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Devis> tousLesDevisTraites() throws Exception {
		Query query = manager.createNamedQuery("devisTraites");
		return query.getResultList();
	}

}
